package exam.xiaozuduikang.exam2019;

import java.util.LinkedList;

public class ptintInt implements Runnable{
    //类公用才行，不然线程互相看不到
    private static Object lock=new Object();
   private static int curNum=1;
   //
    private int threadNo;
    private LinkedList<Integer> queue;
    public ptintInt(LinkedList<Integer> queue, Integer threadNo) {
        this.queue = queue;
        this.threadNo = threadNo;
    }
    @Override
    public void run() {
        while(curNum<101){
            int test=0;
            synchronized (lock){
                //一旦被通知，计算是否当前线程打印，不能用if，if只判断一次
                while(threadNo!=curNum%4){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(threadNo==curNum%4){
                    System.out.println("thread-"+threadNo+"-print:"+curNum);
                    curNum++;
                    lock.notify();
                }else if(0==curNum%4){
                    System.out.println("thread-4-print:"+curNum);
                    curNum++;
                    lock.notifyAll();
                }


            }

        }

    }
}
